package com.czxy.controller;

import com.czxy.rbac.SysRole;
import com.czxy.rbac.SysRolePermission;
import com.czxy.service.SysRolePermissionService;
import com.czxy.service.SysRoleService;
import com.czxy.vo.BaseResult;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private SysRoleService roleService;

    @Autowired
    private SysRolePermissionService rolePermissionService;

    @GetMapping("/all")
    public BaseResult findAll(){
        List<SysRole> list = roleService.list();
        return BaseResult.ok(list);
    }

    @PostMapping("/add")
    public BaseResult addRole(@RequestBody SysRole role){
        boolean flag = roleService.save(role);
        if (flag){
            //保存角色的菜单
           for (Integer permId:role.getPermIds()){
               SysRolePermission rolePermission = new SysRolePermission(role.getId(), permId);
               rolePermissionService.save(rolePermission);
           }
           return BaseResult.ok();
        }
        return BaseResult.error();
    }

    @GetMapping("/{id}")
    public BaseResult findById(@PathVariable("id") Integer id){
        SysRole role = roleService.getById(id);
        return BaseResult.ok(role);
    }



}
